| Conditions | 1 |
| Paths | 1 |
| Total Lines | 102 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 3 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | import { expect } from 'chai'; |
||
| 4 | describe('vm.js', () => { |
||
| 5 | describe('Virtual machine runtime', () => { |
||
| 6 | it('should get the config of an extension', () => { |
||
| 7 | const source = `module.exports = { default: |
||
| 8 | { extension: () => {}, config: { primaryKey: true } } |
||
| 9 | }`; |
||
| 10 | |||
| 11 | return getProperties({ name: 'test-config', source }) |
||
| 12 | .then(result => { |
||
| 13 | expect(result).property('primaryKey').to.be.true; |
||
| 14 | }); |
||
| 15 | }); |
||
| 16 | |||
| 17 | it('should get the alerts of a synchronous extension', () => { |
||
| 18 | const source = 'module.exports = { extension: ctx => ([`Hello, ${ctx.name}!`]) };'; |
||
| 19 | |||
| 20 | return runAndGetAlerts({ name: 'test-alerts', source }, { name: 'Marcelo' }) |
||
| 21 | .then(alerts => { |
||
| 22 | expect(alerts).to.be.an('array'); |
||
| 23 | expect(alerts).property(0).to.be.equals('Hello, Marcelo!'); |
||
| 24 | }); |
||
| 25 | }); |
||
| 26 | |||
| 27 | it('should get the alerts of a asynchronous extension', () => { |
||
| 28 | const source = ` |
||
| 29 | const { delay } = require('bluebird'); |
||
| 30 | |||
| 31 | // Export extension |
||
| 32 | module.exports = { |
||
| 33 | extension: (ctx, done) => { |
||
| 34 | console.log('start'); |
||
| 35 | delay(200).then(() => { |
||
| 36 | done(['It works!']); |
||
| 37 | }); |
||
| 38 | } |
||
| 39 | }; |
||
| 40 | `; |
||
| 41 | |||
| 42 | return runAndGetAlerts({ name: 'test-async-alerts', source }, {}) |
||
| 43 | .then(alerts => { |
||
| 44 | expect(alerts).to.be.an('array'); |
||
| 45 | }); |
||
| 46 | }); |
||
| 47 | }); |
||
| 48 | |||
| 49 | describe('Virtual machine security', () => { |
||
| 50 | it('should not allow process.exit', () => { |
||
| 51 | const source = ` |
||
| 52 | module.exports = { |
||
| 53 | extension: function(context) { |
||
| 54 | this.constructor.constructor('return process')().exit(); |
||
| 55 | return {}; |
||
| 56 | } |
||
| 57 | }; |
||
| 58 | `; |
||
| 59 | |||
| 60 | return runAndGetAlerts({ name: 'test-process-exit', source }, {}) |
||
| 61 | .then(alerts => { |
||
| 62 | throw new Error('Should not fall here'); |
||
| 63 | }) |
||
| 64 | .catch(err => { |
||
| 65 | expect(err).to.be.an.instanceOf(TypeError); |
||
| 66 | }); |
||
| 67 | }); |
||
| 68 | |||
| 69 | it('should not allow file system access', () => { |
||
| 70 | const source = ` |
||
| 71 | const fs = require('fs'); |
||
| 72 | |||
| 73 | module.exports = { |
||
| 74 | extension: () => { |
||
| 75 | console.log(fs); |
||
| 76 | } |
||
| 77 | }; |
||
| 78 | `; |
||
| 79 | |||
| 80 | return runAndGetAlerts({ name: 'test-filesystem-access', source }, {}) |
||
| 81 | .then(alerts => { |
||
| 82 | throw new Error('Should not fall here'); |
||
| 83 | }) |
||
| 84 | .catch(err => { |
||
| 85 | expect(err.message).to.match(/Access denied to require/); |
||
| 86 | }); |
||
| 87 | }); |
||
| 88 | |||
| 89 | it('should refuse extension when it is not a function', () => { |
||
| 90 | const source = ` |
||
| 91 | module.exports = { |
||
| 92 | extension: 1 |
||
| 93 | }; |
||
| 94 | `; |
||
| 95 | |||
| 96 | return runAndGetAlerts({ name: 'test-ext-type', source }, {}) |
||
| 97 | .then(alerts => { |
||
| 98 | throw new Error('Should not fall here'); |
||
| 99 | }) |
||
| 100 | .catch(err => { |
||
| 101 | expect(err.message).to.match(/Expected default exported expression to be a function/); |
||
| 102 | }); |
||
| 103 | }); |
||
| 104 | }); |
||
| 105 | }); |